/**
  @file: PathLocator.java

  PURPOSE: PathLocator provides an interface for a client to obtain access to
      the filesystem paths to files and directories.
      
   
  @author Ron Williams

  REVISION HISTORY ABSTRACT

  PROGRAMMER:  Ron Williams           ORG: MSE

  Copyright 2011 Mission Solutions LLC. All rights reserved.

  END ABSTRACT
 */
package com.mse.utilities;

import java.io.File;

/**
  DESCRIPTION:
      PathLocator provides an interface for a client to obtain access to
      the filesystem paths to files and directories. This class supports
      retrieval of path locations relative to the following configured root
      locations, obtained via Settings.
         - Deployment Root (Deploy_RootDir)
         - CIWi Repository Root (CIWi_REPO_RootDir)
         - CIWi Exchange Root (CIWi_Exchange_RootDir)
         - Crux Exchange Root (CRUX_Exchange_RootDir)

 */
public class PathLocator {
    
    /**
     DESCRIPTION:
     Constructs an instance of a PathLocator. PathLocator information is
     obtained based upon configuration of properties as read by Settings.

    @throws UtilityException - for errors accessing settings
     */
    public PathLocator() throws UtilityException {
        // assemble path fragments used elsewhere (all begin with a slash)
        
        // first assemble all paths from deploy root
        StringBuilder sb  = new StringBuilder();
        Settings      S   = Settings.getInstance();
        String deployRoot = S.getProperty(SettingsKeys.Deploy_RootDir);
        
        if (deployRoot == null) {
            throw new UtilityException("Invalid Deployment Root Dir., " +
            "Settings not configured!");
        }
        
        File aDir = new File(deployRoot);
        if ((! aDir.isDirectory()) || (! aDir.canWrite()) ) {
            throw new UtilityException("Invalid Deployment Root Dir., " +
            "does not exist, or not writeable");
        }
        
        sb  = new StringBuilder(deployRoot);
        sb.append(File.separator);
        sb.append("bin");
        DeployBinDir = sb.toString();
        
        sb  = new StringBuilder(deployRoot);
        sb.append(File.separator);
        sb.append("conf");
        DeployConfDir = sb.toString();
        
        sb  = new StringBuilder(deployRoot);
        sb.append(File.separator);
        sb.append("Apps");
        sb.append(File.separator);       // reuse this path again
        
        StringBuilder aPath = new StringBuilder(sb.toString());
        aPath.append("ciwi");
        aPath.append(File.separator);
        aPath.append("remote");
        CiwiRemoteDir = aPath.toString();
        
        aPath = new StringBuilder(sb.toString());
        aPath.append("ciwi");
        aPath.append(File.separator);
        aPath.append("TXNS");
        CiwiTXNDir = aPath.toString();
        
        aPath = new StringBuilder(sb.toString());
        aPath.append("crux");
        aPath.append(File.separator);
        aPath.append("remote");
        CruxRemoteDir = aPath.toString();
        
        aPath = new StringBuilder(sb.toString());
        aPath.append("crux");
        aPath.append(File.separator);
        aPath.append("TXNS");
        CruxTXNDir = aPath.toString();
        
    }
    
    /**
     DESCRIPTION:
     obtain the path to the CIWi remote executible directory.

      @return path - path to the CIWi remote directory
     */
    public String getCIWiRemotePath() {
        return CiwiRemoteDir;
    }
    
    /**
     DESCRIPTION:
     obtain the CIWi exchange directory path. This is the top level directory
     into which users place CIWi packages for opening, and obtain log files.

      @return path - the path to the CIWi exchange directory
      @throws UtilityException for all errors encountered
     */
    public String getCIWiExchangeDir() throws UtilityException {
        Settings S      = Settings.getInstance();
        String   theDir = S.getProperty(SettingsKeys.CIWi_Exchange_RootDir);
        
        if (theDir == null) {
            throw new UtilityException("Invalid CIWi Exchange Root Dir., " +
            "check configuration.");
        }
        
        File aDir = new File(theDir);
        if ((! aDir.isDirectory()) || (! aDir.canWrite()) ) {
            throw new UtilityException("Invalid CIWi Exchange Root Dir., " +
            "does not exist, or not writeable");
        }
        
        return theDir;
    }
    
    /**
     DESCRIPTION:
     obtain the CIWi repository directory path. This is the top level directory
     into which CIWi will open packages.

      @return path - the path to the CIWi repository directory
      @throws UtilityException for all errors encountered
     */
    public String getCIWiRepoDir() throws UtilityException {
        Settings S      = Settings.getInstance();
        String   theDir = S.getProperty(SettingsKeys.CIWi_REPO_RootDir);
        
        if (theDir == null) {
            throw new UtilityException("Invalid CIWi Repository Root Dir., " +
            "check configuration.");
        }
        
        File aDir = new File(theDir);
        if ((! aDir.isDirectory()) || (! aDir.canWrite()) ) {
            throw new UtilityException("Invalid CIWi Repository Root Dir., " +
            "does not exist, or not writeable");
        }
        
        return theDir;
    }
    
    /**
     DESCRIPTION:
     obtain the CIWi transaction directory path. This is the top level directory
     into which CIWi creates working directories to process user requests.

      @return path - the path to the CIWi Transaction directory
     */
    public String getCIWiTXNDir() {
        return CiwiTXNDir;
    }
    
    /**
     DESCRIPTION:
     This method obtains the directory path to the CIWi local agent
     that performs component installations.
   
     @return the path to the CIWi install local agent script.
     */
    public String getCIWiInstallAgentPath() {
        return DeployBinDir+File.separator+"InstallAgent.sh";
    }
    
    /**
     DESCRIPTION:
     This method obtains the directory path to the CIWi local
     agent that opens package files.
 
     @return the path to the CIWi open package local agent script.
     */
    public String getCIWiOpenPackageAgentPath() {
        return DeployBinDir+File.separator+"OpenPackageAgent.sh";
    }
    
    /**
     DESCRIPTION:
     This method obtains the directory path to the CIWi local
     agent that uninstalls components.

     @return the path to the CIWi uninstall local agent script.
     */
    public String getCIWiUninstallAgentPath() {
        return DeployBinDir+File.separator+"UninstallAgent.sh";
    }
    
    /**
     DESCRIPTION:
     This method obtains the directory path to the CIWi local
     agent that performs remote host cleanup. 

     @return the path to the CIWi cleanup local agent script.
    */
   public String getCIWiCleanupAgentPath() {
      return this.DeployBinDir+File.separator+"CleanupAgent.sh";
   }

   /**
     DESCRIPTION:
     This method obtains the directory path to the CIWi local
     agent that performs remote host cleanup. 

     @return the path to the CIWi cleanup local agent script.
    */
   public String getCIWiGenerateReportAgentPath() {
      return this.DeployBinDir+File.separator+"GenerateReportAgent.sh";
   }
   
   /**
     DESCRIPTION:
     This method obtains the path to the CIWi local agent settings file.
   
     @return the path to the CIWi local agent settings file.
     */
    public String getCIWiAgentSettingsPath() {
        return DeployConfDir+File.separator+"ciwiAgentFileList.txt";
    }
    
    /**
   DESCRIPTION:
   This method obtains the directory path to the Crux local agent

   @return the path to the Crux local agent script.
     */
    public String getCruxLocalAgentPath() {
        return DeployBinDir+File.separator+"CruxAgent.sh";
    }
    
    /**
     DESCRIPTION:
     obtain the path to the Crux remote executable directory.

      @return path - path to the Crux remote directory
     */
    public String getCruxRemotePath() {
        return CruxRemoteDir;
    }
    
    /**
     DESCRIPTION:
     obtain the Crux transaction directory path. This is the top level directory
     into which Crux creates working directories to process user requests.

      @return path - the path to the Crux Transaction directory
     */
    public String getCruxTXNDir() {
        return CruxTXNDir;
    }
    
    /**
     DESCRIPTION:
     obtain the Crux exchange directory path. This is the top level directory
     into which Crux places user log files.

      @return path - the path to the Crux exchange directory
      @throws UtilityException for all errors encountered
     */
    public String getCruxExchangeDir() throws UtilityException {
        Settings S      = Settings.getInstance();
        String   theDir = S.getProperty(SettingsKeys.CRUX_Exchange_RootDir);
        
        if (theDir == null) {
            throw new UtilityException("Invalid Crux Exchange Root Dir., " +
            "check configuration.");
        }
        
        File aDir = new File(theDir);
        if ((! aDir.isDirectory()) || (! aDir.canWrite()) ) {
            throw new UtilityException("Invalid Crux Exchange Root Dir., " +
            "does not exist, or not writeable");
        }
        
        return theDir;
    }
    
    /**
     DESCRIPTION:
     obtain a string representation of this class.

      @return text - description of all paths produced by this class
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(1024);
        
        sb.append("Deployment bin dir: ");
        sb.append(DeployBinDir);
        sb.append("\n  CIWi Remote dir: ");
        sb.append(CiwiRemoteDir);
        sb.append("\n     CIWi TXN dir: ");
        sb.append(CiwiTXNDir);
        sb.append("\n  Crux Remote dir: ");
        sb.append(CruxRemoteDir);
        sb.append("\n     Crux TXN dir: ");
        sb.append(CruxTXNDir);
        sb.append("\n     CIWi Exchange dir: ");
        try {
            sb.append(getCIWiExchangeDir());
            sb.append("\n     CIWi Repository dir: ");
            sb.append(getCIWiRepoDir());
            sb.append("\n     Crux Exchange dir: ");
            sb.append(getCruxExchangeDir());
        } catch (UtilityException ue) {
            sb.append("FAILED to obtain value(s), reason:\n");
            sb.append(ue.toString());
        }
        
        return sb.toString();
    }
    
    //-------------------------------------------------------  PRIVATE
    
    // Deployment tree path fragments
    private String DeployBinDir;
    private String DeployConfDir;
    private String CiwiRemoteDir;
    private String CiwiTXNDir;
    private String CruxRemoteDir;
    private String CruxTXNDir;
    
}
